home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / mod2tutr.zip / CHAP02.TXT < prev    next >
Text File  |  1989-01-18  |  15KB  |  353 lines

  1.  
  2.                                                      Chapter 2
  3.  
  4.                                    GETTING STARTED IN MODULA-2
  5.  
  6.  
  7.  
  8. OUR FIRST MODULA-2 PROGRAM
  9. ______________________________________________________________
  10.  
  11. We are ready to look at our first             ================
  12. instructional program in Modula-2.              PUPPYDOG.MOD
  13. Assuming that you have a full screen          ================
  14. editor of some type, load the program
  15. PUPPYDOG.MOD and display it on your screen or examine a
  16. printed listing of it.  This is an example of the minimum
  17. Modula-2 program.  There is nothing that can be left out of
  18. this program and still have a compilable, executable program.
  19.  
  20. The first word in the program, MODULE, is the name that
  21. identifies a module, and it must be written as given here, in
  22. all capital letters.  During the first two parts of this
  23. tutorial, we will only use this type of a module.  There are
  24. other types, but we will not look at any of them until we get
  25. to part III of this tutorial.  Modula-2 requires us to name
  26. our module so we give it a name, PuppyDog in this case.  We
  27. could have used any name that qualifies as an identifier but
  28. we have chosen a name that has nothing to do with computers
  29. as an illustration that any name could be used.  In a
  30. practical program, you would probably use a name that was
  31. descriptive of the program in some way.
  32.  
  33.  
  34. WHAT IS AN IDENTIFIER?
  35. ______________________________________________________________
  36.  
  37. An identifier is a combination of letters and numbers that
  38. Modula-2 uses to identify a variable, program name, procedure
  39. name, and several other quantities.  In Modula-2, an
  40. identifier is composed of any number of characters.  The
  41. characters may be any mix of alphabetic and numeric
  42. characters, but the first character must be an alphabetic
  43. character.  The case of the alphabetic character is
  44. significant such that PuppyDog, PUPPYDOG, and PuPpYdOg are all
  45. different identifiers.  No spaces or any other special
  46. characters are allowed within an identifier.
  47.  
  48.  
  49. BACK TO THE PROGRAM UNDER CONSIDERATION
  50. ______________________________________________________________
  51.  
  52. The header line is terminated with a semicolon according to
  53. the formal definition of Modula-2.  A semicolon is a statement
  54. separator and many semicolons will be used in large programs.
  55. Following the semicolon, we come to the program itself.  The
  56. program statements are enclosed between the two words BEGIN
  57. and END.  In this case there are no statements, but if there
  58.  
  59.                                                            2-1
  60.  
  61.                        Chapter 2 - Getting Started in Modula-2
  62.  
  63. were some, they would be placed between the two indicated
  64. words.  Finally, the module name is repeated after the END and
  65. it is followed by a period.  The module name is repeated in
  66. order to make the program easier to understand by clearly
  67. marking its limits.  In a program as small as this example,
  68. it really doesn't add to the clarity, but in a large program,
  69. it can be of significant help.  The period marks the end of
  70. the listing and can be thought of as similar to the period
  71. that marks the end of a sentence.
  72.  
  73. The three words, MODULE, BEGIN, and END, are special words in
  74. Modula-2.  They are reserved words because they are used for
  75. a specific purpose and cannot be used in any other way.  They
  76. are not available for your use in any way except for the
  77. defined purpose.  The reserved words in Modula-2 must be
  78. capitalized or the compiler will not consider them as reserved
  79. words.  You will remember that alphabetic characters must have
  80. the correct case in Modula-2.  Some other languages, most
  81. notably Pascal, allow you to use either case anywhere and it
  82. converts them internally so that they are the same.  It would
  83. be permissible for you to use words such as Begin or End as
  84. variables in a Modula-2 program because of the difference in
  85. case, but it would be very poor programming practice and
  86. should be avoided.  We will come across many other reserved
  87. words in these lessons.  There are 40 reserved words in
  88. Modula-2, and they are listed as follows;
  89.  
  90.      AND           ELSIF             LOOP         REPEAT
  91.      ARRAY         END               MOD          RETURN
  92.      BEGIN         EXIT              MODULE       SET
  93.      BY            EXPORT            NOT          THEN
  94.      CASE          FOR               OF           TO
  95.      CONST         FROM              OR           TYPE
  96.      DEFINITION    IF                POINTER      UNTIL
  97.      DIV           IMPLEMENTATION    PROCEDURE    VAR
  98.      DO            IMPORT            QUALIFIED    WHILE
  99.      ELSE          IN                RECORD       WITH
  100.  
  101. We will study each of these words and how to use them in this
  102. tutorial.
  103.  
  104. You should have learned how to use your compiler by now so you
  105. can compile and run this program.  It will do nothing, but
  106. that is significant in itself, because it should at least
  107. return to the operating system after it finishes doing
  108. nothing.  That may sound a little silly, but it does take a
  109. considerable amount of effort to load, transfer control to the
  110. program, and set up linkage back to your Disk Operating
  111. System.
  112.  
  113. It should be noted at this time that the Modula-2 compiler
  114. doesn't care about extra blanks or linefeeds and the careful
  115. programmer will insert extra blanks and linefeeds as desired
  116. in order to make the program easier to read.  As you continue
  117.  
  118.                                                            2-2
  119.  
  120.                        Chapter 2 - Getting Started in Modula-2
  121.  
  122. to program in Modula-2, you will no doubt develop a style of
  123. your own and hopefully your programs can be read and
  124. understood easily by other programmers.
  125.  
  126.  
  127. A PROGRAM THAT DOES SOMETHING
  128. ______________________________________________________________
  129.  
  130. Examine the program named WRITESM.MOD for      ===============
  131. an example of a Modula-2 program that does       WRITESM.MOD
  132. something.  First you should notice that       ===============
  133. the elements of the first program are
  134. still here as they will be in every Modula-2 program.  The
  135. same three reserved words are used here as before, but now
  136. there are some added statements.
  137.  
  138. The third line begins with the reserved word FROM and is a
  139. special line that must be used in any program that accesses
  140. external procedures.  We will not try to define line three at
  141. this time.  We will only say that every external call in
  142. Modula-2 requires a definition of where to find the procedure.
  143. The module named InOut is a Modula-2 collection of input and
  144. output routines that are available for our use and this line
  145. in the program tells the system to look in the InOut
  146. collection for the procedures named WriteLn and WriteString.
  147. When the program needs these particular subprograms to do what
  148. we ask it to do, it knows where to find them.  We will cover
  149. the import list in detail later in this tutorial.  Until then,
  150. simply use the example programs as a guide when you wish to
  151. write a practice program.
  152.  
  153.  
  154. OUR FIRST PROGRAM STATEMENTS
  155. ______________________________________________________________
  156.  
  157. Between the begin and end statements in lines 5 and 19, which
  158. we defined previously as the place where the actual program
  159. is placed, we have a series of WriteString and WriteLn
  160. statements.  These statements are almost self explanatory,
  161. but we will give a few words of explanation about them anyway.
  162. Each line is a call to a procedure which is a very important
  163. feature of Modula-2.  A procedure is a servant that does a
  164. specific job for us in a well defined way.  In the case of the
  165. WriteString, it looks at the string of characters supplied to
  166. it and displays the string of characters on the monitor at the
  167. current cursor position.  The WriteLn procedure serves us by
  168. moving the cursor down one line on the monitor and moving it
  169. to the left side of the screen.
  170.  
  171. The parentheses are required for the WriteString procedure
  172. because it has data following it.  The data within the
  173. parentheses is data supplied to our slave or helper.  It gets
  174. the string of characters between the quotation marks or the
  175. apostrophes and displays the string on the monitor.  You have
  176.  
  177.                                                            2-3
  178.  
  179.                        Chapter 2 - Getting Started in Modula-2
  180.  
  181. a choice of delimiters so that you can output the delimiters
  182. themselves.  If you desire to output a quotation mark to the
  183. monitor, use apostrophes for delimiters, and if you wish to
  184. output apostrophes, use quotation marks.  If you wish to
  185. output both, break the line up and output it piecemeal as in
  186. lines 14 through 16.
  187.  
  188. This program should be very clear to you by now.  First we
  189. tell the system where to get the procedures, then we list the
  190. procedures in the order required to produce the desired
  191. results.  It should be apparent that the lines of the program
  192. between the reserved words BEGIN and END are simply executed
  193. in order.  Compile and execute the program and observe the
  194. output on your monitor.  As mentioned earlier, lines 26
  195. through 29 give the result of executing the program, and your
  196. compiler should give you the identical output.  Lines 24
  197. through 31 are within a comment area and will be ignored by
  198. the compiler.  Comments will be discussed in detail in the
  199. next example program.
  200.  
  201. It should be mentioned at this point that it is possible to
  202. direct the output to the printer or to a disk file but we will
  203. not be doing that for quite some time.  We will stay with the
  204. basic syntax of Modula-2 for now.
  205.  
  206.  
  207. MODULA-2 COMMENTS
  208. ______________________________________________________________
  209.  
  210. No program is complete without a few           ===============
  211. comments embedded in the program as notes        MODCOMS.MOD
  212. to the programmer describing the reasons       ===============
  213. for doing some particular thing.  The
  214. notes are particularly helpful to another programmer who needs
  215. to modify the program some day.  It is not necessary for the
  216. computer to understand the notes.  In fact, you don't want the
  217. computer to try to understand the notes, so you tell the
  218. compiler to ignore the notes completely.  How to do this is
  219. the object of our next program named MODCOMS.MOD which you
  220. should examine at this time.
  221.  
  222. In Modula-2, comments are enclosed in pairs of double
  223. characters.  The comment is started with the (*, and ended
  224. with the *), and can extend over several lines if desired.
  225. The program under consideration at this time, has several
  226. examples of comments in it.  If the comments were completely
  227. removed, the program would be very similar to the last one but
  228. a lot shorter.  Notice that comments can go nearly anywhere
  229. in a program, even before the header statement or after the
  230. ending period.  Comments can even be inserted within a
  231. statement provided they go between tokens.  (See programming
  232. exercise 3 at the end of this chapter.)
  233.  
  234.  
  235.                                                            2-4
  236.  
  237.                        Chapter 2 - Getting Started in Modula-2
  238.  
  239. A DEBUGGING AID
  240. ______________________________________________________________
  241.  
  242. Comments can be used to remove a section of program from
  243. consideration by the compiler so that a particularly
  244. troublesome section of code can be "commented out" until you
  245. solve some of the other problems in program debugging.  This
  246. is illustrated in lines 18 and 19 which are ignored by the
  247. compiler since they are within a comment area.  It is
  248. important to remember that comments can be nested in Modula-2
  249. so that a section of code can be "commented out" even if it
  250. contains other comments.
  251.  
  252. This particular program is not meant to be an example of good
  253. commenting.  It is really a sloppy looking program that would
  254. need some work to put it into a good style, but it does
  255. illustrate where it is possible to put comments.  It should
  256. be clear to you now that the result of execution section at
  257. the end of each program is actually a comment as we mentioned
  258. in the last section.
  259.  
  260.  
  261. GOOD PROGRAMMING STYLE
  262. ______________________________________________________________
  263.  
  264. Examine the program named GOODFORM.MOD for    ================
  265. an example of a well formatted program.         GOODFORM.MOD
  266. Since Modula-2 allows you to use extra        ================
  267. spaces and blank lines freely, you should
  268. use them in any way you desire to make your programs easy to
  269. understand, and therefore easy to debug and modify.  Special
  270. care has been given to style in this program and it paid off
  271. in a very easy to understand program.  Even with your very
  272. limited knowledge of Modula-2 programming, you can very
  273. quickly decipher what it does.  It is so well formatted that
  274. comments are not needed and they would probably detract from
  275. its readability.  No further comment is needed or will be
  276. given.  Compile and run this program to see if it does what
  277. you think it will do.
  278.  
  279.  
  280. REALLY BAD FORMATTING
  281. ______________________________________________________________
  282.  
  283. As you examine UGLYFORM.MOD, you are          ================
  284. seeing an excellent example of bad              UGLYFORM.MOD
  285. formatting.  If you can see at a glance       ================
  286. what this program does, you deserve the
  287. Nobel Prize for understanding software if such a thing exists.
  288. The syntax for this program follows all of the rules of
  289. Modula-2 programming except for good style.  Without saying
  290. anything else about this mess, I would suggest that you try
  291. to compile and run it.  You may be surprised to find that it
  292. does compile and run, and in fact, it is identical to the last
  293.  
  294.                                                            2-5
  295.  
  296.                        Chapter 2 - Getting Started in Modula-2
  297.  
  298. program.  Keep in mind that you can add extra blanks and
  299. linefeeds anyplace you desire in a program to improve its
  300. readability.
  301.  
  302. Hopefully, the last two programs will be an indication to you
  303. that good programming style is important and can be a
  304. tremendous aid in understanding what a program is intended to
  305. do.  You will develop your own programming style as time goes
  306. by.  It is good for you to spend some effort in making your
  307. program look good, but don't get too excited about it yet.
  308. Initially, you should expend your effort in learning how to
  309. program in Modula-2 with reasonable style and strive to
  310. improve your style as you go along.  It would be good for now
  311. if you simply tried to copy the style given in these lessons.
  312.  
  313.  
  314. PROGRAMMING EXERCISES
  315. ______________________________________________________________
  316.  
  317. 1.   Write a program that will display your name on the
  318.      monitor.
  319.  
  320. 2.   Write a program that will display your name on the
  321.      monitor along with your address, city, and state in 3
  322.      separate lines.
  323.  
  324. 3.   Add a comment to the MODCOMS.MOD file between the words
  325.      IMPORT and WriteLn to see if the compiler will allow you
  326.      to put a comment within a statement.
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.                                                            2-6
  352.  
  353.